/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package extension.game.room.handler;

import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;
import extension.game.database.Database;
import extension.game.database.UserData;
import extension.game.room.RoomExtension;
import extension.game.room.Game;
import extension.game.room.Player;
import extension.game.room.GameResponse;
import extension.game.zone.Flag;
import java.util.LinkedList;
import java.util.List;
import log.Debug;

/**
 *
 * @author daz
 */
public class OnUserJoinRoom extends BaseServerEventHandler {

    @Override
    public void handleServerEvent(ISFSEvent event) throws SFSException {
        User user = (User) event.getParameter(SFSEventParam.USER);
        RoomExtension roomExtension = (RoomExtension) getParentExtension();
        Game game = roomExtension.getGame();
        GameResponse response = roomExtension.getResponse();
        if (Debug.isDebug) {
            Player player = game.addPlayer(user.getName(), 1000, user);
            player.setTotalMoney(20000);
            Player[] listPlayers = game.getListPlayers();
            List<String> listNames = new LinkedList<String>();
            List<Long> listMoneys = new LinkedList<Long>();
            List<Integer> listStates = new LinkedList<Integer>();
            for (int i = 0; i < game.getMaxPlayer(); i++) {
                if (listPlayers[i] != null) {
                    listMoneys.add(listPlayers[i].getMoney());
                    listNames.add(listPlayers[i].getName());
                    listStates.add(listPlayers[i].getState());
                } else {
                    listMoneys.add(0l);
                    listNames.add("0");
                    listStates.add(0);
                }
            }
            game.updateTableInfo(listNames, listMoneys, listStates, player);
//            game.sendTableInfo(listNames, listMoneys, listStates, player, user);
//
//            if (game.getNumberPlayings() >= 1 && player.getSeatPos() >= 0) {
//                sendUpdateTable(user, response, player);
//                sendUpdate(roomExtension);
//            }
        } else {
            Database database = roomExtension.getDatabase();
            long moneyJoin = database.getMoneyJoin(roomExtension.getRoomPrefix(), user.getName());
            UserData userData = database.getUserInfo(user.getName());
            Player player = game.addPlayer(user.getName(), moneyJoin, user);
            player.setTotalMoney(userData.getMoney());
            Player[] listPlayers = game.getListPlayers();
            List<String> listNames = new LinkedList<String>();
            List<Long> listMoneys = new LinkedList<Long>();
            List<Integer> listStates = new LinkedList<Integer>();
            for (int i = 0; i < game.getMaxPlayer(); i++) {
                if (listPlayers[i] != null) {
                    listMoneys.add(listPlayers[i].getMoney());
                    listNames.add(listPlayers[i].getName());
                    listStates.add(listPlayers[i].getState());
                } else {
                    listMoneys.add(0l);
                    listNames.add("0");
                    listStates.add(0);
                }
            }
            game.updateTableInfo(listNames, listMoneys, listStates, player);

        }
    }
}
